package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
    "github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    autocodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
    "github.com/flipped-aurora/gin-vue-admin/server/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

type AttributeApi struct {
}

var attributeService = service.ServiceGroupApp.AutoCodeServiceGroup.AttributeService


// CreateAttribute 创建Attribute
// @Tags Attribute
// @Summary 创建Attribute
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Attribute true "创建Attribute"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /attribute/createAttribute [post]
func (attributeApi *AttributeApi) CreateAttribute(c *gin.Context) {
	var attribute autocode.Attribute
	_ = c.ShouldBindJSON(&attribute)
	if err := attributeService.CreateAttribute(attribute); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteAttribute 删除Attribute
// @Tags Attribute
// @Summary 删除Attribute
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Attribute true "删除Attribute"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /attribute/deleteAttribute [delete]
func (attributeApi *AttributeApi) DeleteAttribute(c *gin.Context) {
	var attribute autocode.Attribute
	_ = c.ShouldBindJSON(&attribute)
	if err := attributeService.DeleteAttribute(attribute); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteAttributeByIds 批量删除Attribute
// @Tags Attribute
// @Summary 批量删除Attribute
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Attribute"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /attribute/deleteAttributeByIds [delete]
func (attributeApi *AttributeApi) DeleteAttributeByIds(c *gin.Context) {
	var IDS request.IdsReq
    _ = c.ShouldBindJSON(&IDS)
	if err := attributeService.DeleteAttributeByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateAttribute 更新Attribute
// @Tags Attribute
// @Summary 更新Attribute
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.Attribute true "更新Attribute"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /attribute/updateAttribute [put]
func (attributeApi *AttributeApi) UpdateAttribute(c *gin.Context) {
	var attribute autocode.Attribute
	_ = c.ShouldBindJSON(&attribute)
	if err := attributeService.UpdateAttribute(attribute); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindAttribute 用id查询Attribute
// @Tags Attribute
// @Summary 用id查询Attribute
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocode.Attribute true "用id查询Attribute"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /attribute/findAttribute [get]
func (attributeApi *AttributeApi) FindAttribute(c *gin.Context) {
	var attribute autocode.Attribute
	_ = c.ShouldBindQuery(&attribute)
	if err, reattribute := attributeService.GetAttribute(attribute.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reattribute": reattribute}, c)
	}
}

// GetAttributeList 分页获取Attribute列表
// @Tags Attribute
// @Summary 分页获取Attribute列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocodeReq.AttributeSearch true "分页获取Attribute列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /attribute/getAttributeList [get]
func (attributeApi *AttributeApi) GetAttributeList(c *gin.Context) {
	var pageInfo autocodeReq.AttributeSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := attributeService.GetAttributeInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}
